package ds.tree;

public class LeafCount {
	
	private static class TreeNode{
		int value;
		TreeNode left = null;
		TreeNode right = null;
		
		TreeNode(int val){
			this.value = val;
		}
		
		public String toString(){
			return String.valueOf(value);
		}
	}
	
	public int leafCount (TreeNode node){
		if(node == null){
			return 0;
		}
		else if(	node.left == null && node.right == null	) {
			return 1;
		}
		return leafCount(node.left) + leafCount(node.right);
	}
	
	private static TreeNode tree(){
		TreeNode A = new TreeNode(1);
		TreeNode B = new TreeNode(2);
		TreeNode C = new TreeNode(3);
		TreeNode D = new TreeNode(4);
		TreeNode E = new TreeNode(5);
		
		A.left = B;
		A.right = C;
		B.left = D;
		B.right = E;
		
		return A;
	}
	public static void main(String[] args) {
		LeafCount leafCount = new LeafCount();
		System.out.println(leafCount.leafCount(tree()));
	}

}
